package com.returnsoft.resource.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.resource.dto.ProvinceDto;
import com.returnsoft.resource.eao.ProvinceEao;
import com.returnsoft.resource.entity.Province;
import com.returnsoft.resource.exception.ConversionExcepcion;
import com.returnsoft.resource.exception.EaoExcepcion;
import com.returnsoft.resource.exception.RecruitmentException;
import com.returnsoft.resource.util.Conversion;

/**
 * Session Bean implementation class ProvinceBean
 */
@Stateless
@LocalBean
public class ProvinceBean implements ProvinceInterface {

	@EJB
	private ProvinceEao provinceEao;
	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public ProvinceBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<ProvinceDto> findByDepartment(Integer departmentId)
			throws RecruitmentException {
		try {
			List<Province> provinces = provinceEao
					.findByDepartment(departmentId);
			List<ProvinceDto> provincesDto = new ArrayList<ProvinceDto>();
			for (Province province : provinces) {
				ProvinceDto provinceDto = conv.fromProvince(province);
				provincesDto.add(provinceDto);
			}
			return provincesDto;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

}
